    MODULE globals
    !----------------------------------
    ! Module to declare global variables
    !----------------------------------
    USE parameters

    IMPLICIT NONE
    SAVE



    integer :: decomp_case,ipol_load(numz,numx)
    real*8::beta1,lb_va,ub_va,lb_vt,ub_vt,lb_v,ub_v,x_vp
    real*8:: xx(nparam),maxmin(nparam,2),modelmom(nmom,1),modelmom_nvary(nmom),allmoments2(nvary*nmom),v_prod(numz,numx),v_prod_new(numz,numx),Fs_load(numz,numx)
    real*8, allocatable:: ones(:,:),V_record(:,:,:),Xmatrix(:,:),Xmatrix_new(:,:),XWX(:,:),Xmatrixt(:,:),Ymatrix(:,:),XWY(:,:),XWXinv(:,:),omegaw(:,:),y_vector(:,:),eps_res(:,:)
    real*8::comp_lb(nparam),comp_ub(nparam),  jacob(nmom,nparam),bbeta(2,1),bbeta2(3,1),bbeta3(2,1),bbeta4(3,1),bbeta5(4,1),bbeta6(2,1), bbeta7(2,1),bbeta8(3,1)
    real*8::za(numz),zt(numz),za1(numz,1),s1(1,nums), c1(1,numc),c(numc),s(nums),cc(3,nums),ss(3,nums),c_threshold(numz,numx,numz,numx,nums),error_c(numz,numx,numz,numx,nums), &
        c_threshold2(numz,numx,numz,numx,nums),error_c2(numz,numx,numz,numx,nums),gc(numz,numx,numz,numx),gs(numz,numx,numz,numx),&
        gc_old(numz,numx,numz,numx),gs_old(numz,numx,numz,numx),V(numz,numx),Vnew(numz,numx),Va(numz,numx,numz,numx),Vanew(numz,numx,numz,numx),&
        Vt(numz,numx,numz,numx,3,nums),Vtnew(numz,numx,numz,numx,3,nums),Vtnew_h(numz,numx,numz,numx),Vtnew_m(numz,numx,numz,numx),Vtnew_l(numz,numx,numz,numx),V_interp(numz,numz,numx),&
        Fs(numz,numx), Fscum(numz+1),Fs_record(numz,numx),Fs_old(numz,numx),Fa(numz,numx),Ft(numz,numx),f(numz,numz),Va_2(numz,numx,numz,numx),vv_record2(numz,numx,numz,numx,3,nums), &
        gacp_h(numz,numx,numz,numx,3,nums),gacp_m(numz,numx,numz,numx,3,nums),gacp_l(numz,numx,numz,numx,3,nums),gacp(numz,numx,numz,numx,numc,nums), &
        gacp2(numz,numx,numz,numx,numc,nums),gacp_record(numz,numx,numz,numx,3,numc,nums),temp1b(3,nums),temp2b(3,nums),cc1(3,nums),cc2(3,nums),cc3(3,nums),g1(3,nums),g2(3,nums),g1s,g2s,temp1(3,nums),temp2(3,nums),vv_h(3,nums),vv_m(3,nums), &
        vv_l(3,nums),vv(3,nums),vvc(1,nums),vvs,temp1new_record(numz,numx,numz,numx,numc,nums),p_lb,p_ub,gap, &
        temp2new,temp1l,temp1h,temp1_h,temp1_m,temp1_l, F_exog(numz,numx),trans(numz*numx,numz*numx), trans_tremble(numz*numx,numz*numx),dummy(numz,numx),&
        trans_no_tremble(numz*numx,numz*numx),x(numx),temp1s,temp1s_h,temp1s_m,temp1s_l,temp2s,s_value,c_value,stat_dist(numz*numx,1),stat_dist_new(numz*numx,1),vv_record_big(numz,numx,numz,numx,3,nums,numi),valone_record(numz,numx,numz,numx)
    real*8::be_acq_all(numz,numx,max_iter_dist),dist_record(max_iter_dist),dist_improve(max_iter_dist),change_dist_improve,be_acq_1(numz,numx),be_acq_2(numz,numx)

    real*8::tempa_h, tempa_m,tempa_l, meet_h,meet_m, meet_l,tempt_h, tempt_m,tempt_l, tempa, tempt,value_diff,temp1_record,prob_high_x,f2(numz,numz)
    real*8::temp_alone, V_meet(numz,numx), F_acq(numz,numx),F_tar(numz,numx),growth_store(numz,numx),F_acq_offer(numz,numx),F_tar_offer(numz,numx), F_acq_offer_cum(numz,numx),F_tar_offer_cum(numz,numx),Fs_cum(numz,numx)




    real*8:: equity_ratio(numz,numx,numz,numx),  gc_r(numz,numz,3),  equity_ratio_r(numz,numz,3),gacp_r(numz,numz,3), merger_prob(numz),&
        conditional_accept(numz), equity_ratio_2(numz), equity_ratio_3(numz), offer_dummy(numz),  offer_dummy_2(numz), corner_cash(numz),  corner_equity(numz),&
        interior(numz), nonoffer(numz),stationary_dist_vec(numz,1),helper_var1(numz,numx),helper_var2(numz,numx),helper_var3(numz,numx),new_conditional_accept(numz,numx),new_merger_prob(numz,numx),new_merger_prob_unconditional(numz,numx),new_offer_prob(numz,numx),&
        new_corner_cash(numz,numx),new_corner_equity(numz,numx),new_interior(numz,numx),new_nonoffer(numz,numx),new_equity_ratio_2(numz,numx),helper_equity(numz,numx),helper_equity1(numz,numx),new_equity_ratio_1(numz,numx),&
        realized_gain_det(numz,numx,numz,numx,2,2), realized_gain_det_helper(numz,numx,numz,numx,2,2), equity_ratio_acq(numz,numx,numz,numx,2,2), value_loss(numz,numx,numz,numx,2,2),value_loss2(numz,numx,numz,numx,2,2)

    real*8:: merged_company_value, premerge_acq_value, premerge_tar_value, realized_gain_current,realized_gain_current_dollar, realized_gain(numz,numx), realized_gain_dollar(numz,numx),realized_gain_helper(numz,numx),acq_made_offer_helper(numz,numx), avg_realized_gain, avg_realized_gain_dollar,avg_realized_gain_dollar_unconditional,new_realized_gain(numz,numx),new_realized_gain_dollar(numz,numx), avg_realized_gain_helper, avg_value_loss,agg_value_loss, avg_value_loss_helper
    real*8:: avg_fixedcost_ratio,fixedcost_ratio(numz,numx)

    integer::indexs(numz,numx,numz,numx),indexc(numz,numx,numz,numx),indexs_old(numz,numx,numz,numx),indexc_old(numz,numx,numz,numx),&
        offermade(numz,numx,numz,numx),offermade_old(numz,numx,numz,numx),idxc(1,nums),idxs,i_lb,i_ub,gvary, zx_idx,a,b,be_acq_guess(numz,numx),be_acq(numz,numx),&
        indexs_r(numz,numz,3),indexc_r(numz,numz,3),  gacp_eqm(numz,numx,numz,numx,3), gacp_eqm_perfect(numz,numx,numz,numx), gacp_eqm_i(numz,numx,numz,numx,3,numi)


    real*8::     agg_rnd_intensity,consumption,agg_rnd, output,gc_perfect(numz,numx,numz,numx),gs_perfect(numz,numx,numz,numx),Vanew_perfect(numz,numx,numz,numx),Vtnew_perfect(numz,numx,numz,numx),Ftp(numz,numx),equity_ratio_perfect(numz,numx,numz,numx),prob_acq(numz,numx)
    integer:: indexs_perfect(numz,numx,numz,numx),indexc_perfect(numz,numx,numz,numx),offermade_perfect(numz,numx,numz,numx)
    real*8:: tempa_perfect,meet_h_perfect,meet_m_perfect,meet_l_perfect,tempt_h_perfect,tempt_m_perfect,tempt_l_perfect,tempt_perfect,tempa_combined,tempt_combined

    real*8:: wfhusseya(numz,1),transz(numz,numz),epstemp(numz,1),atemp(numz,1),p2(3,numz) !,p(3)
    real*8::   acq_ann_ret(numz,numx,numz,numx),    cash_ratio(numz,numx,numz,numx),    log_mv_ratio(numz,numx,numz,numx),    acq_ann_ret_helper(numz,numx,numz,numx),&
        avg_acq_ann_ret,    avg_acq_ann_ret_helper, tar_ann_ret(numz,numx,numz,numx), temp_tar_ann_ret, avg_tar_ann_ret, avg_tar_ann_ret_helper, tar_frac_merger_gain(numz,numx,numz,numx), avg_tar_frac_merger_gain, avg_tar_frac_merger_gain_helper, &
        rel_size(numz,numx,numz,numx), avg_rel_size, avg_rel_size_helper, avg_rnd_intensity, tar_frac_of_gain(numz,numx), avg_tar_frac_of_gain,  avg_innovation,avg_i_policy,avg_growth_rate, z_alone_begin(numz,numx), z_alone_end(numz,numx), z_alone_helper(numz,numx)

    real*8:: F_exog_corrected

    

    real*8::   i_cost(numi), mu_value(numi),p3(3,numz,numi),p3_cum(3,numz,numi),gc_i(numz,numx,numz,numx,numi), gs_i(numz,numx,numz,numx,numi), Vtnew_h_i(numz,numx,numz,numx,numi), Vtnew_m_i(numz,numx,numz,numx,numi),&
        Vtnew_l_i(numz,numx,numz,numx,numi), V_meet_i(numz,numx,numi),Vanew_i(numz,numx,numz,numx,numi),Vnew_i(numz,numx,numi), mve_reg_begin(numz,numx), mve_reg_end(numz,numx), mve_reg_helper(numz,numx)
    integer:: indexc_i(numz,numx,numz,numx,numi), indexs_i(numz,numx,numz,numx,numi),offermade_i(numz,numx,numz,numx,numi), i_policy(numz,numx),i_policy_new(numz,numx)

    real*8:: mean_log_mve, std_log_mve, std_eps_res,diff_pol
    real*8::vv_record_big_3(numz,numx,numz,numx,3,nums,3)

    real*8::ss_f(numc,nums),cc_f(numc,nums),cc1_f(numc,nums),cc2_f(numc,nums),cc3_f(numc,nums),&
        g1_f(numc,nums),g2_f(numc,nums), temp1_f(numc,nums),temp2_f(numc,nums),temp1b_f(numc,nums),temp2b_f(numc,nums),&
        vv_h_f(numc,nums),vv_m_f(numc,nums),vv_l_f(numc,nums),vv_f(numc,nums),&
        vvc_f(1,nums),vvs_f,Vanew_f(numz,numx,numz,numx),gc_f(numz,numx,numz,numx),gs_f(numz,numx,numz,numx),gc_i_f(numz,numx,numz,numx,numi),gs_i_f(numz,numx,numz,numx,numi)

    real*8, allocatable:: vv_record_big_f_short(:,:,:,:,:,:),vv_record_big_f(:,:,:,:,:,:,:), gacp_h_f(:,:,:,:,:,:),gacp_m_f(:,:,:,:,:,:),gacp_l_f(:,:,:,:,:,:),&
        Vtnew_h_f(:,:,:,:,:,:,:),Vtnew_m_f(:,:,:,:,:,:,:),Vtnew_l_f(:,:,:,:,:,:,:),Vtnew_h_f_short(:,:,:,:,:,:),Vtnew_m_f_short(:,:,:,:,:,:),Vtnew_l_f_short(:,:,:,:,:,:)



    integer:: idxs_f,idxc_f(1,nums),indexs_f(numz,numx,numz,numx),indexc_f(numz,numx,numz,numx), offermade_f(numz,numx,numz,numx),&
        indexs_i_f(numz,numx,numz,numx,numi),indexc_i_f(numz,numx,numz,numx,numi),offermade_i_f(numz,numx,numz,numx,numi),Vanew_i_f(numz,numx,numz,numx,numi)


    real*8::mean_z_acq,mean_z_tar,mean_z_all,mean_z_acq2tar,mean_vnk_acq,mean_vnk_tar,mean_vnk,mean_v_acq,mean_v_tar,mean_v, mean_v_acq2tar, mean_vnk_acq2tar, mean_mtb_acq2tar,mean_mtb,mean_mtb_tar,mean_mtb_acq
    real*8::mean_z_acq_log,mean_z_tar_log,mean_z_all_log,mean_z_acq2tar_log,mean_vnk_acq_log,mean_vnk_tar_log,mean_vnk_log,mean_v_acq_log,mean_v_tar_log,mean_v_log, mean_v_acq2tar_log, mean_vnk_acq2tar_log, &
        mean_mtb_acq2tar_log,mean_mtb_log,mean_mtb_tar_log,mean_mtb_acq_log
    integer::median_idx_acq,median_idx_tar,median_idx_all
    real*8::  median_z_acq,median_z_tar,median_z_all,median_v_acq,median_v_tar,median_v,median_vnk_acq,median_vnk_tar,median_vnk,median_mtb_acq,median_mtb_tar,median_mtb
    END MODULE globals

